package com.oocl.login.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class LoginFilter implements Filter {

    
    public LoginFilter() {
    }

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest)request;
		HttpServletResponse rep = (HttpServletResponse)response;
		
			String requestUri = req.getRequestURI();
            String contextPath = req.getContextPath();
	        String url = requestUri.substring(contextPath.length());
	        System.out.println(url);
			if("/login.jsp".equals(url)||"/login".equals(url)||"/register.jsp".equals(url)||"/regist".equals(url)||"/logo.png".equals(url)){
				chain.doFilter(request, response);
			}else{
				//System.out.println(req.getSession().getAttribute("name"));
				if(req.getSession().getAttribute("name")!=null){
					chain.doFilter(request, response);
				}else{
					rep.sendRedirect("login.jsp");
				}
			}
	}

	
	public void init(FilterConfig fConfig) throws ServletException {
		
	}

}
